﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Windows.Forms;
using SpicIE;

namespace AutoFillPluginIE
{
    [ComVisible(true),
    ClassInterfaceAttribute(ClassInterfaceType.AutoDual),
    GuidAttribute(AutoFillPluginIE.PLUGIN_GUID),
    ProgIdAttribute(AutoFillPluginIE.PLUGIN_PROGID)]
    public class AutoFillPluginIE : SpicIE.Host
    {
        public const string PLUGIN_GUID = "e984fe7b-3c5a-4c2f-af84-8ee981d37a3f";
        public const string PLUGIN_PROGID = "AutoFillPluginIE.SpicIE";

        public static AutoFillPluginIE HostInstance = null;

        public AutoFillPluginIE()
            : base()
        {
            HostInstance = this;
            this.OnDocumentComplete += AutoFillPluginIE_OnDocumentComplete;
            ForceShowToolbar(typeof(AutoFillToolbar));
        }

        void AutoFillPluginIE_OnDocumentComplete(object pDisp, ref object url)
        {
            try
            {
                
                //if (url.ToString() == "http://www.google.com.vn/")
                //{
                //    SHDocVw.IWebBrowser2 browser = HostInstance.BrowserRef as SHDocVw.IWebBrowser2;
                //    if (browser != null)
                //    {
                //        // get the IHTMLDocument interface
                //        mshtml.IHTMLDocument2 htmlDoc = browser.Document as mshtml.IHTMLDocument2;
                //        mshtml.HTMLDocument htmlDoc2 = browser.Document as mshtml.HTMLDocument;
                //        if (htmlDoc != null)
                //        {

                //            mshtml.HTMLInputElement search = (mshtml.HTMLInputElement)htmlDoc2.getElementById("q");
                //            search.setAttribute("value", "test", 0);
                //            /*
                //            mshtml.IHTMLElementCollection submits = htmlDoc2.getElementsByName("btnG");
                //            MessageBox.Show(submits.length.ToString());
                //            mshtml.HTMLInputElement submit = submits.item(0,0) as mshtml.HTMLInputElement;
                //            submit.click();
                //             */ 
                            
                //        }
                //    }
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


        #region Register your new browser UI elements here

        internal static List<SpicIE.Controls.IControlBase> RunOnceCOMRegistration()
        {
            Host.TraceSink.TraceEvent(TraceEventType.Information, 0, "RunOnceRegisterCOMControls");

            List<SpicIE.Controls.IControlBase> controls = new List<SpicIE.Controls.IControlBase>();
            controls.Add(new AutoFillToolbar());



            return controls;
        }

        #endregion

        #region SpicIE - Required methods, do not change

        [ComRegisterFunction]
        internal static void RegisterControls(Type typeToRegister)
        {
            SpicIE.Host.RegisterHost(typeToRegister, PLUGIN_GUID, PLUGIN_PROGID);

            SpicIE.Host.RegisterControls(RunOnceCOMRegistration());
        }

        [ComUnregisterFunction]
        internal static void UnregisterControls(Type typeToRegister)
        {
            SpicIE.Host.UnregisterHost(typeToRegister, PLUGIN_GUID);

            SpicIE.Host.UnregisterControls(RunOnceCOMRegistration());
        }

        #endregion

    }
}
